package cn.bfay.lion.rest.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import cn.bfay.lion.rest.service.BaseService;
import cn.bfay.lion.rest.client.rsp.ProductDetailInfoRsp;
import cn.bfay.lion.rest.client.rsp.ProductInfoRsp;
import cn.bfay.lion.rest.entity.UserProduct;
import cn.bfay.lion.rest.manager.JingdongManager;
import cn.bfay.lion.rest.mapper.UserProductMapper;
import cn.bfay.lion.rest.model.UserSession;
import cn.bfay.lion.rest.service.ProductService;
import cn.bfay.lion.rest.vo.ProductDetailInfoPackageVo;
import cn.bfay.lion.rest.vo.ProductDetailInfoVo;
import cn.bfay.lion.rest.vo.ProductInfoVo;
import cn.bfay.web.result.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * MESSAGE.
 *
 * @author wangjiannan
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ProductServiceImpl extends BaseService implements ProductService {
    private final UserProductMapper userProductMapper;
    private final JingdongManager jingdongManager;

    @Override
    public PageResult<ProductInfoVo> getProductInfoByPaging(String name, Long pageNum, Long pageSize) {
        PageResult<ProductInfoRsp> productInfoRspPageInfo =
            jingdongManager.getProductInfoByPaging(name, pageNum, pageSize);

        UserSession userSession = getUserSession();
        List<UserProduct> userProductList = null;
        if (userSession != null) {
            userProductList = userProductMapper.selectList(new QueryWrapper<UserProduct>().lambda()
                .eq(UserProduct::getOpenid, userSession.getOpenid())
                .eq(UserProduct::getMark, (byte) 1));
        }
        return converVo(productInfoRspPageInfo, userProductList);
    }

    private PageResult<ProductInfoVo> converVo(PageResult<ProductInfoRsp> productInfoRspPageInfo,
                                               List<UserProduct> userProductList) {
        PageResult<ProductInfoVo> productInfoVoPageResult =
            new PageResult<>(productInfoRspPageInfo.getCurrentPage(), productInfoRspPageInfo.getPageSize(),
                productInfoRspPageInfo.getTotalPage(), productInfoRspPageInfo.getTotalCount(),
                productInfoRspPageInfo.getItems(), ProductInfoVo.class);
        for (ProductInfoVo productInfoVo : productInfoVoPageResult.getItems()) {
            productInfoVo.setMark((byte) 0);
            productInfoVo.setMarkDesc("关注");

            if (CollectionUtils.isNotEmpty(userProductList)) {
                List<String> skuids = userProductList.stream().map(UserProduct::getSkuid).collect(Collectors.toList());
                if (skuids.contains(productInfoVo.getSkuid())) {
                    productInfoVo.setMark((byte) 1);
                    productInfoVo.setMarkDesc("取消关注");
                }
            }
        }
        return productInfoVoPageResult;
    }


    @Override
    public ProductDetailInfoPackageVo getProductDetailInfoPackage(String skuid) {
        ProductDetailInfoPackageVo productDetailInfoPackageVo = new ProductDetailInfoPackageVo();
        ProductInfoRsp productInfoRsp = jingdongManager.getProductInfoBySkuid(skuid);
        productDetailInfoPackageVo.setName(productInfoRsp.getName());
        String clickUrl = jingdongManager.getPromotionCommon(skuid);
        productDetailInfoPackageVo.setClickUrl(clickUrl);
        List<ProductDetailInfoRsp> productDetailInfoRsps = jingdongManager.getProductDetailInfos(skuid);
        if (CollectionUtils.isNotEmpty(productDetailInfoRsps)) {
            List<ProductDetailInfoVo> productDetailInfoVos = new ArrayList<>(productDetailInfoRsps.size());
            productDetailInfoRsps.forEach(productDetailInfoRsp -> {
                ProductDetailInfoVo productDetailInfoVo = new ProductDetailInfoVo();
                BeanUtils.copyProperties(productDetailInfoRsp, productDetailInfoVo);
                productDetailInfoVos.add(productDetailInfoVo);
            });
            productDetailInfoPackageVo.setProductDetailInfoVos(productDetailInfoVos);
            setMinPrice(productDetailInfoPackageVo, productDetailInfoVos);
        }
        return productDetailInfoPackageVo;
    }

    private void setMinPrice(ProductDetailInfoPackageVo productDetailInfoPackageVo,
                             List<ProductDetailInfoVo> productDetailInfoVos) {
        double minNormalPrice = productDetailInfoVos.stream()
            .min(Comparator.comparingDouble(ProductDetailInfoVo::getFinalNormalPrice)).orElse(new ProductDetailInfoVo())
            .getFinalNormalPrice();
        double minPlusPrice = productDetailInfoVos.stream()
            .min(Comparator.comparingDouble(ProductDetailInfoVo::getFinalPlusPrice)).orElse(new ProductDetailInfoVo())
            .getFinalPlusPrice();
        productDetailInfoPackageVo.setMinNormalPrice(minNormalPrice);
        productDetailInfoPackageVo.setMinPlusPrice(minPlusPrice);
    }
}
